<?php


/*
 * Admin moze dodawac wszystkich uzytkownikow
 * 
 */
class Admin_AccountController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction()
    {
        $users = new Application_Model_Dao_User();
        $this->view->users = $users->getAll()->toArray();
    }

    public function addAction()
    {
    	$form = new Application_Form_Account();
    	$form->appendValidationEngineFiles($this->view); 
    	$polish = BH_TranslatePL::getPolishTranslation();
  		$translate = new Zend_Translate('array', $polish, 'pl');
  		$form->setTranslator($translate);
    	
    	$form->submit->setLabel('Dodaj nowego użytkownika');
		//$this->view->form = $form;
    	
    	$request = $this->getRequest();
    	if ($request->isPost()) {
    		if ($form->isValid($request->getPost())) {
    			$user = new Application_Model_Entities_User();
    			$user->setAcademicDegree( $form->getValue('academicDegreeID') );
    			$user->setName( $form->getValue('name') );
    			$user->setSurname( $form->getValue('surname') );
    			$user->setEmail( $form->getValue('email') );
    			$user->setPassword( $form->getValue('password') );
    			$user->setCathedral( $form->getValue('cathedralID') );
    			$user->setRole( $form->getValue('roleID') );
    			$userDao = new Application_Model_Dao_User();
    			$userDao->saveUser($user);
    		//	$result = Zend_Auth::getInstance()->authenticate($user);
    			//if ($result->isValid()) {
    				$this->_helper->redirector('index');
    			} else {
    				//$form->login->addError('Należy wypełnić wszystkie pola.');
    			}
    		//}
    	}
    	$this->view->AccountForm = $form;
    }

    public function editAction()
    {
    	$form = new Application_Form_Account();
    	$form->appendValidationEngineFiles($this->view); 
    	$form->submit->setLabel('Zapisz zmiany');
		$this->view->form = $form;
		
    	$request = $this->getRequest();
    	if ($request->isPost()) {
    		if ($form->isValid($request->getPost())) {
    			$user = new Application_Model_Entities_User();
    			$user->setAcademicDegree( $form->getValue('academicDegreeID') );
    			$user->setName( $form->getValue('name') );
    			$user->setSurname( $form->getValue('surname') );
    			$user->setEmail( $form->getValue('email') );
    			$user->setPassword( $form->getValue('password') );
    			$user->setCathedral( $form->getValue('cathedralID') );
    			$user->setRole( $form->getValue('roleID') );
    			$userDao = new Application_Model_Dao_User();
    			$userDao->saveUser($user);
    		//	$result = Zend_Auth::getInstance()->authenticate($user);
    			//if ($result->isValid()) {
    				$this->_helper->redirector('index');
    			} else {
    				$form->login->addError('Należy wypełnić wszystkie pola.');
    			}
		} else {
			$id = $this->_getParam('id', 0);
			if ($id > 0) {
				$userDao = new Application_Model_Dao_User();
				$form->populate($userDao->getUserByID($id));  // coś z populate nie gra
			} 
		}
    }

    public function deleteAction()
    {
    	
    }

}



